package com.jago;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import java.io.FileInputStream;

public class ExcelReadTest {

    private static final String PATH = "D:\\Program Files (x86)\\IDEA-2020.3.4\\workspace\\excel-demo\\excel\\";

    @Test
    public void test03Version() throws Exception {
        FileInputStream fis = new FileInputStream(PATH + "poi.xls");
        Workbook workbook = new HSSFWorkbook(fis);
        Sheet sheet = workbook.getSheetAt(0);
        Row row = sheet.getRow(0);
        Cell cell = row.getCell(0);
        System.out.println(cell.getStringCellValue());
        fis.close();
    }

    @Test
    public void test07Version() throws Exception {
        FileInputStream fis = new FileInputStream(PATH + "poi.xlsx");
        Workbook workbook = new XSSFWorkbook(fis);
        Sheet sheet = workbook.getSheetAt(0);
        Row row = sheet.getRow(0);
        Cell cell = row.getCell(0);
        System.out.println(cell.getStringCellValue());
        fis.close();
    }

    @Test
    public void testCellType() throws Exception {
        FileInputStream fis = new FileInputStream(PATH + "student.xlsx");
        Workbook workbook = new XSSFWorkbook(fis);
        Sheet sheet = workbook.getSheetAt(0);
        Row row = sheet.getRow(0);
        int count = row.getPhysicalNumberOfCells();
        for (int i = 0; i < count; i++) {
            Cell cell = row.getCell(i);
            if (cell != null) {
                System.out.print(cell.getStringCellValue() + " | ");
            }
        }

        fis.close();
    }

}
